package cn.tansanqinger.service.knowledge.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.catalogue.CatalogueSqlDaoImpl;
import cn.tansanqinger.dao.sql.knowledge.KnowledgeSqlDaoImpl;
import cn.tansanqinger.pojo.Admin;
import cn.tansanqinger.pojo.Catalogue;
import cn.tansanqinger.pojo.Knowledge;
import cn.tansanqinger.service.knowledge.IKnowledgeService;
import cn.tansanqinger.util.Tools;

@Service
public class KnowledgeServiceImpl implements IKnowledgeService{
	
	@Autowired
	private KnowledgeSqlDaoImpl impl;
	@Autowired
	private CatalogueSqlDaoImpl cimpl;

	@Override
	public Map<String, Object> selectMessage(HttpServletRequest request) {
		String keywords = Tools.isEmpty(request.getParameter("keywords"));
		String kispopup = Tools.isEmpty(request.getParameter("kispopup"));
		String option = Tools.isEmpty(request.getParameter("option"));
		String page = Tools.isEmpty(request.getParameter("page"));
		String limit = Tools.isEmpty(request.getParameter("limit"));
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int kispopupInt = -1;
		if(!"".equals(kispopup)) {
			kispopupInt = Integer.parseInt(kispopup);
			if(kispopupInt<0||kispopupInt>1) {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据错误");
				return resultMap;
			}
		}
		if("".equals(option)) {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
			return resultMap;
		}
		int pageInt = 1;
		if(!"".equals(page)) {
			pageInt = Integer.parseInt(page);
		}
		int limitInt = 10;
		if(!"".equals(limit)) {
			limitInt = Integer.parseInt(limit);
		}
		return impl.selectMessage(keywords, kispopupInt, Integer.parseInt(option), pageInt, limitInt);
	}

	@Override
	public Map<String, Object> serialNumber() {
		Integer csn = impl.serialNumber();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code","0");
		map.put("msg", csn);
		return map;
	}

	@Override
	public Map<String, Object> add(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Admin admin = (Admin) request.getSession().getAttribute("userMessage");
		String cno = Tools.isEmpty(request.getParameter("cno"));
		if(!cno.matches("\\d+")) {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
			return resultMap;	
		}
		Catalogue catalogue = cimpl.selectOnt(Integer.parseInt(cno));
		Knowledge knowledge = new Knowledge();
		int sn = impl.serialNumber();
		knowledge.setKsn(sn);
		knowledge.setKtitle(catalogue.getCname());
		knowledge.setKmessage(Tools.isEmpty(request.getParameter("kmessage")));
		knowledge.setKimg(Tools.isEmpty(request.getParameter("kimg")));
		knowledge.setKispopup(0);
		knowledge.setKisquery(0);
		knowledge.setKjurisdiction(0);
		knowledge.setKauid(admin.getAid());
		knowledge.setKnumber(0);
		knowledge.setKcollectnnumber(0);
		knowledge.setKcomment(0);
		knowledge.setKaudit(1);
		knowledge.setKisreport(0);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		knowledge.setKtime(format.format(new Date()));
		knowledge.setKqtime(format.format(new Date()));
		if(impl.add(knowledge)==1) {
			catalogue.setCpath(impl.selectSn(sn).getKid()+"");
			cimpl.modification(catalogue);
			resultMap.put("code","0");
			resultMap.put("msg", "添加成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "添加失败");
		}
		return resultMap;	
	}

	@Override
	public Map<String, Object> updateStatus(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")) {
			Knowledge object = impl.selectOnt(Integer.parseInt(id));
			object.setKispopup(object.getKispopup()==0?1:0);
			if(impl.updateStatus(object)==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "修改成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "修改失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public Knowledge selectIdOne(String id) {
		return impl.selectOnt(Integer.parseInt(id));
	}

	@Override
	public Map<String, Object> delete(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id.matches("\\d+")) {
			if(impl.deleteOne(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> modification(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(!id.matches("\\d+")) {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
			return resultMap;	
		}
		Knowledge knowledge = impl.selectOnt(Integer.parseInt(id));
		if(knowledge!=null) {
			knowledge.setKmessage(Tools.isEmpty(request.getParameter("kmessage")));
			if(impl.modification(knowledge)==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "修改成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "修改失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

}
